package Sevlet;

import bean.SignUp;
import bean.TourRoute;
import bean.TourShift;
import util.SignUpDao;
import util.TourRouteDao;
import util.TourShiftDao;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.*;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

public class signUp extends HttpServlet {

    public void success(){
        String msg="恭喜你！报名成功！";
        int Type= JOptionPane.YES_NO_CANCEL_OPTION;
        String title="信息提示";
        JOptionPane.showMessageDialog(null, msg, title, Type);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String type = new String(req.getParameter("type").getBytes("ISO-8859-1"),"UTF-8");


        if (type.equals("routeNo")){

            String routeNo = new String(req.getParameter("routeNo").getBytes("ISO-8859-1"),"UTF-8");
            //存报名信息
            TourRouteDao trdao = new TourRouteDao();
            TourRoute tr = new TourRoute();
            try {
                tr = trdao.get(routeNo);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            SignUp  sgUp = new SignUp();
            sgUp.tourRouteSet(tr);
            HttpSession session = req.getSession();
            session.setAttribute("SignUp", sgUp);

            //跳转下一个页面
            TourShiftDao tsdao = new TourShiftDao();
            List<TourShift> TSList = null;
            try {
                TSList = tsdao.fromRoute(routeNo);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            System.out.println("part 1 TSList.size="+TSList.size());
            session.setAttribute("TSList", TSList);
            System.out.println("part 1 发射成功~");
            resp.sendRedirect("tourist/tourShift.jsp");

        }else if (type.equals("shiftNo")){

            String shiftNo = new String(req.getParameter("shiftNo").getBytes("ISO-8859-1"),"UTF-8");
            //存报名信息
            TourShiftDao tsdao = new TourShiftDao();
            TourShift ts = new TourShift();
            try {
                ts = tsdao.get(shiftNo);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            HttpSession session = req.getSession();
            SignUp  sgUp = (SignUp) session.getAttribute("SignUp");
            if (sgUp != null){
                sgUp.tourShiftSet(ts);
            }else {
                sgUp = new SignUp();
                sgUp.tourShiftSet(ts);
            }
            //跳转报名表信息
            System.out.println("part 2 发射成功~");
            resp.sendRedirect("tourist/signUp.jsp");
        }else if (type.equals("signup")){
            SignUp  sgUp = (SignUp) req.getSession().getAttribute("SignUp");
            SignUpDao sgupdao = new SignUpDao();
            try {
                sgUp.setSignUpNo(sgupdao.updateSignUpNo());
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                sgupdao.addSignUp(sgUp);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            //添加成功，跳转回报名页面
            //success();
            resp.sendRedirect("index.jsp");
        }
    }
}
